package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：我的游记
 *
 * @package: cn.wolfcode.trip.api.controller
 * @author: Patch
 * @date: 2019/5/21 23:50
 * 总结:
 */
@RestController
@RequestMapping("travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    /**
     * 新增和修改游记
     * 1.请求路径 /travels
     * 2.请求方式 post
     * 3.接受参数 Travel
     * 4.返回
     */
    @PostMapping
    public Object save(Travel travel) {
        JsonResult result = new JsonResult();
        try {
            travelService.saveOrUpdate(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("亲，操作失败");
        }
        return result;
    }

    /**
     * 获取指定id的游记
     * 1.请求路径 /travels/{id}
     * 2.请求方式 get
     * 3.接受参数 id
     * 4.返回 Travel
     */
    @GetMapping("{id}")
    public Travel getById(@PathVariable("id") Long id) {
        return travelService.get(id);
    }

    /**
     * 游记更新接口
     * @param travel
     * @return
     */
    @PutMapping
    public Object update(Travel travel){
        JsonResult result = new JsonResult();
        try {
            travelService.saveOrUpdate(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("亲，操作失败");
        }
        return result;
    }

    /**
     * 获取全部游记的分页数据接口/高级查询
     */
    @GetMapping
    public PageInfo query(TravelQueryObject qo){
        // 已发布状态
        qo.setState(Travel.STATE_RELEASE);
        // 公开
        qo.setIsPublic(true);
        // 排序
        qo.setOrderBy("t.releaseTime desc");
        return travelService.query(qo);
    }



}
